//
//  CalendarLogic.m
//  TestCalander
//
//  Created by yull on 14/12/1.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "CalendarLogic.h"

@implementation CalendarLogic

+ (NSInteger)getDayCountInYear:(NSInteger)year month:(NSInteger)month
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month;
    NSDate *date = [calendar dateFromComponents:components];
    
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
    return range.length;
}

+ (NSInteger)getWeekCountInYear:(NSInteger)year month:(NSInteger)month
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month;
    NSDate *date = [calendar dateFromComponents:components];
    
    NSRange range = [calendar rangeOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
    return range.length;
}

//从星期天开始
+ (NSInteger)getFirstWeekInYear:(NSInteger)year month:(NSInteger)month
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month;
    components.day = 1;
    NSDate *date = [calendar dateFromComponents:components];
    
    NSInteger firstWeek = [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekOfMonthCalendarUnit forDate:date];
    return firstWeek;
}
@end
